package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Assessment;
import com.ruoyi.system.domain.bo.AssessmentBo;
import com.ruoyi.system.domain.vo.AssessmentVo;
import com.ruoyi.system.mapper.AssessmentMapper;
import com.ruoyi.system.service.IAssessmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 勤务考核Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-16
 */
@RequiredArgsConstructor
@Service
public class AssessmentServiceImpl implements IAssessmentService {

    private final AssessmentMapper baseMapper;

    /**
     * 查询勤务考核
     */
    @Override
    public AssessmentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询勤务考核列表
     */
    @Override
    public TableDataInfo<AssessmentVo> queryPageList(AssessmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Assessment> lqw = buildQueryWrapper(bo);
        Page<AssessmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询勤务考核列表
     */
    @Override
    public List<AssessmentVo> queryList(AssessmentBo bo) {
        LambdaQueryWrapper<Assessment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Assessment> buildQueryWrapper(AssessmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Assessment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOfficerId() != null, Assessment::getOfficerId, bo.getOfficerId());
        lqw.eq(bo.getDutyDate() != null, Assessment::getDutyDate, bo.getDutyDate());
        lqw.eq(bo.getScore() != null, Assessment::getScore, bo.getScore());
        lqw.eq(StringUtils.isNotBlank(bo.getAssessmentType()), Assessment::getAssessmentType, bo.getAssessmentType());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), Assessment::getRemarks, bo.getRemarks());
        return lqw;
    }

    /**
     * 新增勤务考核
     */
    @Override
    public Boolean insertByBo(AssessmentBo bo) {
        Assessment add = BeanUtil.toBean(bo, Assessment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改勤务考核
     */
    @Override
    public Boolean updateByBo(AssessmentBo bo) {
        Assessment update = BeanUtil.toBean(bo, Assessment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Assessment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除勤务考核
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
